package student.service;

import student.Main;
import student.dto.TeacherDto;
import student.utils.FileUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.stream.Collectors;


public class TeacherService {
    /**
     * 删除一个老师
     */
    public void deleteById(Integer id) throws Exception {

        // 这是自定义异常
        if (id >= Main.teacherDtos.size()) {
            throw new Exception("Id 输入错误");
        }
        //过滤掉 需要删除的id
        Main.teacherDtos = Main.teacherDtos.stream().filter(st -> !Objects.equals(st.getId(), id)).collect(Collectors.toList());
    }


    private void delete() {
        FileUtil fileUtil = new FileUtil();
        fileUtil.deleteFile(Main.teacherFilePath);
        List<String> strings = getStrings();
        try {
            fileUtil.write(Main.teacherFilePath, strings);
        } catch (IOException e) {
            System.out.println("删除失败");
            e.printStackTrace();
        }
    }

    private List<String> getStrings() {
        List<String> strings = new ArrayList<>();
        strings.add("ID,老师姓名,老师年龄,老师工号");
        for (int i = 0; i < Main.teacherDtos.size(); i++) {
            TeacherDto teacherDto = Main.teacherDtos.get(i);
            int index = i + 1;

            strings.add(index + "," + teacherDto.getName() + "," + teacherDto.getAge() + "," + teacherDto.getNum());
        }

        return strings;
    }

    /**
     * 修改书籍名称
     */
    public void updateName() {
        FileUtil fileUtil = new FileUtil();
        fileUtil.queryAll(Main.teacherFilePath);
        System.out.println("----------请输入 需要修改老师的Id---------");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();
        TeacherDto teacherDto = Main.teacherDtos.get(id - 1);

        System.out.println("ID,姓名,年龄,工号");
        System.out.println(teacherDto.getId() + "," + teacherDto.getName() + "," + teacherDto.getAge() + "," + teacherDto.getNum());
        System.out.println("----------请输入 需要修改后的老师名称---------");
        String name = sc.next();
        Main.teacherDtos.get(id - 1).setName(name);

        this.delete();
        System.out.println("----------修改成功  ---------");
        fileUtil.queryAll(Main.teacherFilePath);
    }


    /**
     * 添加 一个学生  在队尾
     */
    public void add() {
        FileUtil fileUtil = new FileUtil();
        System.out.println("----------请输入 老师姓名---------");

        TeacherDto teacherDto = new TeacherDto();
        Scanner sc = new Scanner(System.in);
        teacherDto.setName(sc.next());
        System.out.println("----------请输入 老师年龄---------");
        teacherDto.setAge(sc.nextInt());
        System.out.println("----------请输入 老师工号---------");
        teacherDto.setNum(sc.next());
        Main.teacherDtos.add(teacherDto);

        this.delete();
        System.out.println("----------添加成功---------");
        fileUtil.queryAll(Main.teacherFilePath);
    }

    public void beck() {
        File srcFile = new File(Main.teacherFilePath);
        if (!srcFile.exists()) {
            System.out.println("模板文件不存在");
        }

        //创建备份文件
        File backUpFile = new File(Main.teacherFilePath + ".bak");
        try {
            if (backUpFile.createNewFile()) {
                //创建备份文件成功，进行文件复制
                fileCopy(srcFile, backUpFile);
            }
        } catch (Exception e) {
            System.out.println("备份文件失败");
        }

        System.out.println("-------------备份成功------------------");
    }

    public static void fileCopy(File srcFile, File destFile) throws Exception {
        InputStream src = new BufferedInputStream(new FileInputStream(srcFile));
        OutputStream dest = new BufferedOutputStream(new FileOutputStream(destFile));
        byte[] trans = new byte[1024];
        int count = -1;
        while ((count = src.read(trans)) != -1) {
            dest.write(trans, 0, count);
        }
        dest.flush();
        src.close();
        dest.close();
    }
}
